import { URL, fileURLToPath } from 'node:url'
import { defineConfig } from 'vite'
import { configDefaults } from 'vitest/config'
import unocss from 'unocss/vite'

import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueI18n from '@intlify/unplugin-vue-i18n/vite'
import vueDevTool from 'vite-plugin-vue-devtools'

import autoImport from 'unplugin-auto-import/vite'
import components from 'unplugin-vue-components/vite'
import { ArcoResolver } from 'unplugin-vue-components/resolvers'

import { markdown } from '../plugins/markdown'
import { removeTagProperties } from '../plugins/remove-tag-properties'

export const resolve = {
  alias: {
    '@': fileURLToPath(new URL('../src', import.meta.url)),
    '~': fileURLToPath(new URL('../src/modules', import.meta.url))
  }
}

export default defineConfig({
  base: '/',
  optimizeDeps: {
    include: [
      '@arco-design/web-vue/es/**/index.js',
      'vue',
      'ofetch',
      'pinia',
      '@vueuse/core',
      '@tanstack/vue-query',
      'vue-i18n'
    ]
  },
  build: {
    cssMinify: 'lightningcss'
  },
  css: {
    transformer: 'postcss',
    preprocessorOptions: {
      less: {
        modifyVars: {
          'arcoblue-6': '#80AE8A',
          'dark-arcoblue-6': '#80AE8A',
          'green-6': '#619B7A'
        },
        javascriptEnabled: true
      }
    }
  },
  define: {
    __VUE_OPTIONS_API__: true, // Vue 3 的 options API, arco 组件库需要
    __INTLIFY_DROP_MESSAGE_COMPILER__: true // 优化 vue-i18n 的 JIT 编译
  },

  plugins: [
    autoImport({
      imports: [],
      resolvers: [ArcoResolver()],
      dts: fileURLToPath(new URL('../src/types/auto-imports.d.ts', import.meta.url))
    }),
    components({
      resolvers: [ArcoResolver({ sideEffect: true, importStyle: 'less' })],
      dts: fileURLToPath(new URL('../src/types/components.d.ts', import.meta.url))
    }),
    markdown(),
    unocss(),
    removeTagProperties({
      enabledModes: ['production'],
      removeProperties: ['data-test', 'data-e2e']
    }),
    vue({ include: [/\.vue$/, /\.md$/] }),
    vueJsx(),
    vueI18n({
      include: [fileURLToPath(new URL('../src/**/**/locales/*.json', import.meta.url))],
      dropMessageCompiler: true
    }),
    vueDevTool()
  ],
  resolve,

  test: {
    coverage: {
      all: true,
      include: ['src/']
    },
    environment: 'happy-dom',
    exclude: [...configDefaults.exclude, 'e2e/*', 'playwright']
  }
})
